

#include "mpi_lib.h"
#include "mpi.h"
#include "mpi_vi.h"
#include "mpi_cmd.h"
#include "sensor_common.h"
#include "mpi_sys.h"

u32 sgks_mpi_vi_CalcVideoMode(int video_width, int video_heigh, int video_fps)
{
    u32 video_mode = 0;

    if (video_width==0 || video_heigh==0 || video_fps == 0)
    {
        return SGKS_MPI_VIDEO_MODE_AUTO;
    }
    
    video_mode = SGKS_MPI_VIDEO_MODE(video_width, video_heigh, video_fps, 0);

    return video_mode;
}

int sgks_mpi_vi_GetSensorCapability(sgks_vi_src_capability_s *vi_cap_ability)
{

    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();

    int ret = 0;
    int i = 0;
    u8* msg_buf;

    CHECK_POINT_NULL_RETURN(vi_cap_ability, SGKS_ERR_VI_PARAM_ERR);
    CHECK_POINT_NULL_RETURN(mpi, SGKS_FAIL);

    SGKS_SENSOR_DRV_BUFF(msg_buf);

    for (i=0; i<mpi->vi_device.vi_device_num; i++)
    {
        if(mpi->vi_device.vi_deviceinfo[i].enable == 1)
        {

            SGKS_SENSOR_DRV_CLEAN_DATABUFF();

            SGKS_SENSOR_DRV_IOCTL(SGKS_SENSOR_OPERATION_GETCAPABILITY, 0, sizeof(sgks_vi_src_capability_s), mpi->vi_device.vi_deviceinfo[i].device_id,SGKS_ERR_SENSOR_DRIVERIOCTLERR);

            memcpy(&vi_cap_ability[i], msg_buf, sizeof(sgks_vi_src_capability_s));

        }

    }

    return SGKS_SUCCESS;

}


int sgks_mpi_vi_CloseDeviceNode(sgks_mpi_manager_s *mpi)
{
    CHECK_POINT_NULL_RETURN(mpi,SGKS_FAIL);

    int ret = 0;
    int i = 0;

    CHECK_POINT_NULL_RETURN(mpi, SGKS_ERR_VI_PARAM_ERR);

    sgks_mpi_vi_device_s *vi_device 	= &mpi->vi_device;
    u32 curr_module_id					= mpi->curr_module_id;

    for (i=0; i<vi_device->vi_device_num; i++)
    {
        if(mpi->sensor_handle[i][curr_module_id])
        {
			close(mpi->sensor_handle[i][curr_module_id]);
			mpi->sensor_handle[i][curr_module_id] = -1;
		}
    }

    return SGKS_SUCCESS;
}


int sgks_mpi_vi_OpenDeviceNode(sgks_mpi_manager_s *mpi)
{
    CHECK_POINT_NULL_RETURN(mpi,SGKS_FAIL);

    int ret = 0;
    int i = 0;

    CHECK_POINT_NULL_RETURN(mpi, SGKS_ERR_VI_PARAM_ERR);

    sgks_mpi_vi_device_s *vi_device 	= &mpi->vi_device;
    u32 curr_module_id					= mpi->curr_module_id;

    for (i=0; i<vi_device->vi_device_num; i++)
    {
        if((ret = open(vi_device->vi_deviceinfo[i].device_node, O_RDWR, 0)) < 0)
        {
            Printf("err open mpi driver dev:%s\n", vi_device->vi_deviceinfo[i].device_node);
            return SGKS_ERR_SYS_OPENDRIVERDEVERR;
        }

        mpi->sensor_handle[i][curr_module_id] = ret;
    }

    return SGKS_SUCCESS;
}


int sgks_mpi_vi_EnalbeDevice(sgks_mpi_vi_device_s *vi_device, u32 *sensor_handle)
{

    int i = 0;

    u8* msg_buf;
    u8  vin_sel = 0;

    SGKS_VI_STREAM_START_S *p_stream;

    CHECK_POINT_NULL_RETURN(vi_device, SGKS_ERR_VI_PARAM_ERR);
    CHECK_POINT_NULL_RETURN(sensor_handle, SGKS_ERR_VI_PARAM_ERR);
  
    SGKS_SENSOR_DRV_BUFF(msg_buf);

#if 0
    SGKS_SENSOR_DRV_CLEAN_DATABUFF();
    *((int *)msg_buf) = 1;
    SGKS_SENSOR_DRV_IOCTL(SGKS_SENSOR_OPERATION_SET_PRINT_DEFAULT_REG, 0, sizeof(int), 0,SGKS_ERR_SENSOR_DRIVERIOCTLERR);
#endif
    ///////////////////////////////////////////////////////////
    //reset sensor
    ///////////////////////////////////////////////////////////
    SGKS_SENSOR_DRV_IOCTL(SGKS_SENSOR_OPERATION_INIT_HW, 0, sizeof(u32), 0,SGKS_ERR_SENSOR_DRIVERIOCTLERR);

    SGKS_SENSOR_DRV_CLEAN_DATABUFF();
    p_stream = (SGKS_VI_STREAM_START_S *)msg_buf;
    p_stream->num = vi_device->vi_device_num;
    for (i=0; i<vi_device->vi_device_num; i++)
    {
        p_stream->stream_info[i].enable          = vi_device->vi_deviceinfo[i].enable;
        p_stream->stream_info[i].input_interface = vi_device->vi_deviceinfo[i].interface_type;
        p_stream->stream_info[i].input_type      = vi_device->vi_deviceinfo[i].vi_type;
        p_stream->stream_info[i].mirror_mode     = vi_device->vi_deviceinfo[i].mirror_mode;
        p_stream->stream_info[i].bayer_pattern   = vi_device->vi_deviceinfo[i].bayer_pattern;
        p_stream->stream_info[i].cap_start_x     = vi_device->vi_deviceinfo[i].cap_start_x;
        p_stream->stream_info[i].cap_start_y     = vi_device->vi_deviceinfo[i].cap_start_y;     
        p_stream->stream_info[i].cap_width       = vi_device->vi_deviceinfo[i].cap_width;
        p_stream->stream_info[i].cap_height      = vi_device->vi_deviceinfo[i].cap_height;             
        p_stream->stream_info[i].video_mode      = sgks_mpi_vi_CalcVideoMode(vi_device->vi_deviceinfo[i].vi_width, 
                                                                          vi_device->vi_deviceinfo[i].vi_height, 
                                                                          vi_device->vi_deviceinfo[i].vi_fps);

    }

    SGKS_SENSOR_DRV_IOCTL(SGKS_SENSOR_OPERATION_START, 0, sizeof(SGKS_VI_STREAM_START_S), 0,SGKS_ERR_SENSOR_DRIVERIOCTLERR);

    return SGKS_SUCCESS;
}

int sgks_mpi_vi_UpdateSourceBuff(sgks_mpi_manager_s *mpi)
{
    sgks_vi_src_capability_s vi_cap_ability[MAX_VI_NUM];
    sgks_vi_src_capability_s *current_vi_info  = NULL;
    int ret = sgks_mpi_vi_GetSensorCapability(vi_cap_ability);   

    CHECK_POINT_NULL_RETURN(mpi, SGKS_ERR_VI_PARAM_ERR);

    if (ret<0)
    {
        return SGKS_FAIL;
    }
    mpi->system_source_buff[0].format.width  = vi_cap_ability[0].cap_width;
    mpi->system_source_buff[0].format.height = vi_cap_ability[0].cap_height;

    return 0;
}

int sgks_mpi_vi_init(sgks_mpi_vi_device_s *vi_device)
{
    int ret = 0;


    /**************************/
    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();

    CHECK_POINT_NULL_RETURN(vi_device, SGKS_ERR_VI_PARAM_ERR);
    CHECK_POINT_NULL_RETURN(mpi, SGKS_FAIL);
    
    if (sgks_mpi_sys_GetModuleInitStatus(mpi ,MPI_MODULE_VIN_ID) == MPI_MODULE_STATUS_INIT)
    {
        Printf("sgks_mpi_vi already init\n");
        return SGKS_SUCCESS;
    }

    mpi->curr_module_id		= MPI_MODULE_VIN_ID;

    ret = sgks_mpi_sys_GetDriverHandle(mpi);
    if (ret != SGKS_SUCCESS)
        return ret;

    memcpy(&mpi->vi_device, vi_device, sizeof(sgks_mpi_vi_device_s));

    ret = sgks_mpi_vi_OpenDeviceNode(mpi);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    ret = sgks_mpi_vi_EnalbeDevice(&mpi->vi_device, mpi->sensor_handle);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    ret = sgks_mpi_sys_UpdateModuleInitStatus(mpi, MPI_MODULE_VIN_ID, MPI_MODULE_STATUS_INIT);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    ret = sgks_mpi_vi_UpdateSourceBuff(mpi);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    Printf("sgks_mpi_vi_init ok!\n");

    return SGKS_SUCCESS;

}




u32 sgks_mpi_vo_FpsFormatToVfr(u32 fps, u32 format)
{
    if((fps == SGKS_VIDEO_FPS_29_97) && (format == SGKS_VIDEO_FORMAT_INTERLACE))
    {
        return SGKS_DSP_VFR_29_97_INTERLACE;
    }

    if((fps == SGKS_VIDEO_FPS_29_97) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_29_97_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_59_94) && (format == SGKS_VIDEO_FORMAT_INTERLACE))
    {
        return SGKS_DSP_VFR_59_94_INTERLACE;
    }

    if((fps == SGKS_VIDEO_FPS_59_94) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_59_94_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_23_976) && (format == SGKS_VIDEO_FORMAT_INTERLACE))
    {
        return SGKS_DSP_VFR_23_976_INTERLACED;
    }

    if((fps == SGKS_VIDEO_FPS_23_976) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_23_976_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_12_5) && (format == SGKS_VIDEO_FORMAT_INTERLACE))
    {
        return SGKS_DSP_VFR_12_5_INTERLACED;
    }

    if((fps == SGKS_VIDEO_FPS_12_5) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_12_5_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_6_25) && (format == SGKS_VIDEO_FORMAT_INTERLACE))
    {
        return SGKS_DSP_VFR_6_25_INTERLACED;
    }

    if((fps == SGKS_VIDEO_FPS_6_25) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_6_25_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_3_125) && (format == SGKS_VIDEO_FORMAT_INTERLACE))
    {
        return SGKS_DSP_VFR_3_125_INTERLACED;
    }

    if((fps == SGKS_VIDEO_FPS_3_125) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_3_125_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_7_5) && (format == SGKS_VIDEO_FORMAT_INTERLACE))
    {
        return SGKS_DSP_VFR_7_5_INTERLACED;
    }

    if((fps == SGKS_VIDEO_FPS_7_5) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_7_5_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_3_75) && (format == SGKS_VIDEO_FORMAT_INTERLACE))
    {
        return SGKS_DSP_VFR_3_75_INTERLACED;
    }

    if((fps == SGKS_VIDEO_FPS_3_75) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_3_75_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_25) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_25_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_30) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_30_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_24) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_24_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_15) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_15_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_50) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_50_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_60) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_60_PROGRESSIVE;
    }

    if((fps == SGKS_VIDEO_FPS_120) && (format == SGKS_VIDEO_FORMAT_PROGRESSIVE))
    {
        return SGKS_DSP_VFR_120_PROGRESSIVE;
    }

    if(format == SGKS_VIDEO_FORMAT_INTERLACE)
    {
        return SGKS_DSP_VFR_INTERLACE(DIV_ROUND(512000000, fps));
    }
    else if(format == SGKS_VIDEO_FORMAT_PROGRESSIVE)
    {
        return SGKS_DSP_VFR(DIV_ROUND(512000000, fps));
    }

    return SGKS_DSP_VFR_29_97_PROGRESSIVE;


}


int sgks_mpi_vi_isp_senseor(u32 id, u8 *arg, int size)
{

    //Printf("sgks_mpi_vi_isp_senseor 0x%x size %d\n",id,size);

    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();
    int ret = 0;
    int i = 0;
    u8* msg_buf;

    CHECK_POINT_NULL_RETURN(arg, SGKS_ERR_VI_PARAM_ERR);
    CHECK_POINT_NULL_RETURN(mpi, SGKS_FAIL);
    
    SGKS_SENSOR_DRV_BUFF(msg_buf);

    memcpy(msg_buf, arg, size);


    SGKS_SENSOR_DRV_IOCTL(id, 0, size, 0,SGKS_ERR_SENSOR_DRIVERIOCTLERR);

    memcpy(arg, msg_buf, size);



    return SGKS_SUCCESS;

}

int sgks_mpi_vi_GetFramerateTime(u32 fps)
{
    u32 time = -1;

    switch(fps)
    {
    case SGKS_VI_FPS_AUTO:
        time = SGKS_VIDEO_FPS_AUTO;
        break;
    case SGKS_VI_FPS_1:
        time = SGKS_VIDEO_FPS_1;
        break;
    case SGKS_VI_FPS_2:
        time = SGKS_VIDEO_FPS_2;
        break;
    case SGKS_VI_FPS_3:
        time = SGKS_VIDEO_FPS_3;
        break;
    case SGKS_VI_FPS_3_125:
        time = SGKS_VIDEO_FPS_3_125;
        break;
    case SGKS_VI_FPS_3_75:
        time = SGKS_VIDEO_FPS_3_75;
        break;
    case SGKS_VI_FPS_4:
        time = SGKS_VIDEO_FPS_4;
        break;
    case SGKS_VI_FPS_5:
        time = SGKS_VIDEO_FPS_5;
        break;
    case SGKS_VI_FPS_6:
        time = SGKS_VIDEO_FPS_6;
        break;
    case SGKS_VI_FPS_6_25:
        time = SGKS_VIDEO_FPS_6_25;
        break;
    case SGKS_VI_FPS_7_5:
        time = SGKS_VIDEO_FPS_7_5;
        break;
    case SGKS_VI_FPS_10:
        time = SGKS_VIDEO_FPS_10;
        break;
    case SGKS_VI_FPS_12:
        time = SGKS_VIDEO_FPS_12;
        break;
    case SGKS_VI_FPS_12_5:
        time = SGKS_VIDEO_FPS_12_5;
        break;
    case SGKS_VI_FPS_13:
        time = SGKS_VIDEO_FPS_13;
        break;
    case SGKS_VI_FPS_14:
        time = SGKS_VIDEO_FPS_14;
        break;
    case SGKS_VI_FPS_15:
        time = SGKS_VIDEO_FPS_15;
        break;
    case SGKS_VI_FPS_20:
        time = SGKS_VIDEO_FPS_20;
        break;
    case SGKS_VI_FPS_24:
        time = SGKS_VIDEO_FPS_24;
        break;
    case SGKS_VI_FPS_25:
        time = SGKS_VIDEO_FPS_25;
        break;
    case SGKS_VI_FPS_29_97:
        time = SGKS_VIDEO_FPS_29_97;
        break;
    case SGKS_VI_FPS_30:
        time = SGKS_VIDEO_FPS_30;
        break;
    case SGKS_VI_FPS_50:
        time = SGKS_VIDEO_FPS_50;
        break;
    case SGKS_VI_FPS_59_94:
        time = SGKS_VIDEO_FPS_59_94;
        break;
    case SGKS_VI_FPS_60:
        time = SGKS_VIDEO_FPS_60;
        break;
    case SGKS_VI_FPS_120:
        time = SGKS_VIDEO_FPS_120;
        break;
    default:
        time = -1;
        break;
    }

    return time;
}



void sgks_vi_Cmd_SensorConfig(sgks_mpi_manager_s *mpi)
{
    sgks_dsp_cmd_sensor_input_setup_s dsp_cmd;

    sgks_vi_src_capability_s vi_cap_ability[MAX_VI_NUM];

    if (mpi==NULL)
    {
        return SGKS_ERR_VI_PARAM_ERR;
    }

    int ret = sgks_mpi_vi_GetSensorCapability(vi_cap_ability);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    sgks_vi_src_capability_s *current_vi_info = &vi_cap_ability[0];


    memset(&dsp_cmd, 0, sizeof(dsp_cmd));

    dsp_cmd.cmd_code 				= SGKS_DSP_CMD_SENSOR_INPUT_SETUP;
    dsp_cmd.sensor_id 				= current_vi_info->sensor_id;
    dsp_cmd.field_format 			= 1;
    dsp_cmd.sensor_resolution 		= current_vi_info->bit_resolution;
    dsp_cmd.sensor_pattern 			= current_vi_info->bayer_pattern;
    dsp_cmd.first_line_field_0 		= 0;
    dsp_cmd.first_line_field_1 		= 0;
    dsp_cmd.first_line_field_2 		= 0;
    dsp_cmd.first_line_field_3 		= 0;
    dsp_cmd.first_line_field_4 		= 0;
    dsp_cmd.first_line_field_5 		= 0;
    dsp_cmd.first_line_field_6 		= 0;
    dsp_cmd.first_line_field_7 		= 0;
    dsp_cmd.sensor_readout_mode 	= 0;



    mpi_cmd_hex(dsp_cmd, cmd_code);
    mpi_cmd(dsp_cmd, sensor_id);
    mpi_cmd(dsp_cmd, field_format);
    mpi_cmd(dsp_cmd, sensor_resolution);
    mpi_cmd(dsp_cmd, first_line_field_0);
    mpi_cmd(dsp_cmd, first_line_field_1);
    mpi_cmd(dsp_cmd, first_line_field_2);
    mpi_cmd(dsp_cmd, first_line_field_3);
    mpi_cmd(dsp_cmd, first_line_field_4);
    mpi_cmd(dsp_cmd, first_line_field_5);
    mpi_cmd(dsp_cmd, first_line_field_6);
    mpi_cmd(dsp_cmd, first_line_field_7);
    mpi_cmd(dsp_cmd, sensor_readout_mode);

    ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(sgks_dsp_cmd_sensor_input_setup_s));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

}

int sgks_mpi_sys_ViBindVo(int vi_id, int vo_id)
{
    int ret = 0;
    int i;
    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();
    u8 src_type = 1;
    sgks_dsp_cmd_switch_src_s dsp_cmd;

    CHECK_POINT_NULL_RETURN(mpi, SGKS_FAIL);
	
	if (mpi->mpi_init_param.dsp_init_param.operation_mode == SGKS_DSP_DECODE_MODE)
	{
		return 0;
	}

    for (i=0; i<mpi->vi_device.vi_device_num; i++)
    {
        if (mpi->vi_device.vi_deviceinfo[i].device_id== vi_id &&
            mpi->vi_device.vi_deviceinfo[i].enable == 1)
        {
            if (mpi->vi_device.vi_deviceinfo[i].vi_type== SGKS_VI_TYPE_DMA)
                src_type = 2; // dma in
            else // if (mpi->vi_device.vi_deviceinfo[i].dma_type == SGKS_VI_DMA_ISP)
                src_type = 1; // vin

            break;
        }
    }

    dsp_cmd.cmd_code = SGKS_DSP_CMD_IPCAM_SWITCH_SRC;
    dsp_cmd.switch_src = src_type;

    ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(sgks_dsp_cmd_switch_src_s));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    mpi_cmd_hex(dsp_cmd, cmd_code);
    mpi_cmd(dsp_cmd, switch_src);

    return SGKS_SUCCESS;    
}

int sgks_mpi_vi_SetMirrorMode(sgks_mpi_vi_MirrorMode_e mirror_mode)
{
    int ret = 0;
    int i;
    int mirror = (int)mirror_mode; 
    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();
    sgks_mdi_main_mpi_s *main_mpi = NULL;

    CHECK_POINT_NULL_RETURN(mpi, SGKS_ERR_VI_PARAM_ERR);

    main_mpi = &mpi->isp_CB;
    CHECK_POINT_NULL_RETURN(main_mpi, SGKS_FAIL);

    ret = main_mpi->isp_ConfigSensor(SGKS_SENSOR_OPERATION_SET_MIRROR_MODE, &mirror, sizeof(mirror));

    return ret;    
}

int sgks_mpi_vi_SetBayerPattern(sgks_mpi_vi_BayerPattern_e bayer_pattern)
{
    int ret = 0;
    int i;
    int bayer = (int)bayer_pattern;
    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();
    sgks_mdi_main_mpi_s *main_mpi = NULL;

    CHECK_POINT_NULL_RETURN(mpi, SGKS_ERR_VI_PARAM_ERR);    

    main_mpi = &mpi->isp_CB;
    CHECK_POINT_NULL_RETURN(main_mpi, SGKS_FAIL); 
    
    ret = main_mpi->isp_ConfigSensor(SGKS_SENSOR_OPERATION_SET_BAYER_PATTERN, &bayer, sizeof(bayer));

    return ret;    
}


int sgks_mpi_vi_GetSupportResolution(sgks_mpi_vi_resolution_s *resolution)
{
    int ret = 0;
    int i;
    sgks_mpi_manager_s *mpi = mpi_get_managerHanle();
    sgks_mdi_main_mpi_s *main_mpi = NULL;

    CHECK_POINT_NULL_RETURN(resolution, SGKS_ERR_VI_PARAM_ERR);    
    CHECK_POINT_NULL_RETURN(mpi, SGKS_FAIL);    

    main_mpi = &mpi->isp_CB;
    CHECK_POINT_NULL_RETURN(main_mpi, SGKS_FAIL);

    ret = main_mpi->isp_ConfigSensor(SGKS_SENSOR_OPERATION_GET_ALL_VIDEO_MODE, resolution, sizeof(sgks_mpi_vi_resolution_s));

    return ret;    
}

u32 sgks_mpi_vi_GetSensorID(void)
{
    return gadi_get_sensors_id();
}

int sgks_mpi_vi_Cmd_VideoPreProc(sgks_mpi_manager_s *mpi)
{
    sgks_dsp_cmd_video_preproc_s dsp_cmd;
    
    u32 oversampling_disabled = 0;

    int i;
    int dma_en = 0;

    CHECK_POINT_NULL_RETURN(mpi, SGKS_ERR_VI_PARAM_ERR); 

    sgks_vi_src_capability_s vi_cap_ability[MAX_VI_NUM];

    sgks_source_buffer_format_ex_t *main_format = &mpi->system_source_buff[0].format;

    int ret = sgks_mpi_vi_GetSensorCapability(vi_cap_ability);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    sgks_vi_src_capability_s *current_vi_info = &vi_cap_ability[0];

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code       			    = SGKS_DSP_CMD_VIDEO_PREPROCESSING;
    dsp_cmd.input_format   			    = current_vi_info->input_format;
    dsp_cmd.sensor_id     				= current_vi_info->sensor_id;
    dsp_cmd.bit_resolution 				= current_vi_info->bit_resolution;
    dsp_cmd.bayer_pattern  				= current_vi_info->bayer_pattern;

    if(current_vi_info->video_format == SGKS_VIDEO_FORMAT_INTERLACE)
    {
        dsp_cmd.vi_frame_rate 			= sgks_mpi_vo_FpsFormatToVfr(current_vi_info->frame_rate * 2, SGKS_VIDEO_FORMAT_PROGRESSIVE);
    }
    else
    {
        dsp_cmd.vi_frame_rate 			= sgks_mpi_vo_FpsFormatToVfr(current_vi_info->frame_rate,SGKS_VIDEO_FORMAT_PROGRESSIVE);
    }

    dsp_cmd.sensor_readout_mode 		= current_vi_info->sensor_readout_mode;
    dsp_cmd.vidcap_w            		= current_vi_info->cap_width;
    dsp_cmd.vidcap_h            		= current_vi_info->cap_height;
    dsp_cmd.input_center_x      		= current_vi_info->cap_width << 15;
    dsp_cmd.input_center_y      		= current_vi_info->cap_height << 15;
    dsp_cmd.input_center_x      		= 0;
    dsp_cmd.input_center_y      		= 0;
    dsp_cmd.zoom_factor_x       		= 0;
    dsp_cmd.zoom_factor_y       		= 0;

    //skip first frame to avoid bad frame capture cause DSP VI crash
    dsp_cmd.vid_skip            		= 8;
    dsp_cmd.main_w              		= main_format->width;
    dsp_cmd.main_h              		= main_format->height;
    dsp_cmd.encode_x            		= 0;
    dsp_cmd.encode_y            		= 0;
    dsp_cmd.encode_w            		= main_format->width;
    dsp_cmd.encode_h            		= main_format->height;

    dsp_cmd.preview_A_en 				= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_A].enable;
    dsp_cmd.preview_w_A 				= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_A].output_width;  //原始缩放
    dsp_cmd.preview_h_A 				= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_A].output_height;
    dsp_cmd.preview_format_A       		= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_A].format;
    dsp_cmd.preview_frame_rate_A     	= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_A].fps;


    dsp_cmd.preview_B_en 				= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_B].enable;
    dsp_cmd.preview_w_B 				= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_B].output_width;  //原始缩放
    dsp_cmd.preview_h_B 				= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_B].output_height;
    dsp_cmd.preview_format_B 			= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_B].format;
    dsp_cmd.preview_frame_rate_B 		= mpi->preview_config.preview_info[SGKS_VO_PREVIEW_B].fps;


    dsp_cmd.keep_states               	= 0;
    dsp_cmd.horizontal_channel_number 	= 0;
    dsp_cmd.vertical_channel_number   	= 0;

    if(mpi->system_source_buff[0].format.width > MAX_WIDTH_IN_FULL_FPS)
    {
        oversampling_disabled 			= 1;
    }
    else
    {
        oversampling_disabled 			= mpi->system_resource.oversampling_disable;
    }

    dsp_cmd.oversampling_disabled 		= oversampling_disabled;
    dsp_cmd.hd_sdi_mode 				= mpi->system_resource.hd_sdi_mode;

    for (i=0; i<mpi->vi_device.vi_device_num; i++)
    {
        if (mpi->vi_device.vi_deviceinfo[i].vi_type== SGKS_VI_TYPE_DMA&&
            mpi->vi_device.vi_deviceinfo[i].enable == 1)
        {
            dma_en = 1;
            break;
        }
    }

    if(dma_en)
    {
        int dma_in_type = 0;
        dsp_cmd.cvbs_input_width      = mpi->vi_device.vi_deviceinfo[i].cap_width;
        dsp_cmd.cvbs_input_height     = mpi->vi_device.vi_deviceinfo[i].cap_height;
        dsp_cmd.cvbs_interleave       = 0; 

        if (mpi->vi_device.vi_deviceinfo[i].interface_type==SGKS_VI_INTERFACE_DVP)
        {
            dma_in_type = SGKS_PREVIEW_SRC_TYPE_LVDS;
        }
        else if (mpi->vi_device.vi_deviceinfo[i].interface_type==SGKS_VI_INTERFACE_MIPI)
        {
            dma_in_type = SGKS_PREVIEW_SRC_TYPE_MIPI;
        }
        else
        {
            dma_in_type = SGKS_PREVIEW_SRC_TYPE_CVBS;
        }
        dsp_cmd.prevC_src = dma_in_type;
        
        //printf("[%s] DMA ENABLE\n", __FUNCTION__);
        //if(mpi->vi_device.vi_deviceinfo[i].dma_prv_en)
        {
            dsp_cmd.prevB_src                 = dma_in_type;//SGKS_PREVIEW_SRC_TYPE_LVDS;//;
            dsp_cmd.preview_src_w_B           = mpi->vi_device.vi_deviceinfo[i].cap_width;
            dsp_cmd.preview_src_h_B           = mpi->vi_device.vi_deviceinfo[i].cap_height;
            dsp_cmd.preview_src_x_offset_B    = mpi->vi_device.vi_deviceinfo[i].cap_start_x;
            dsp_cmd.preview_src_y_offset_B    = mpi->vi_device.vi_deviceinfo[i].cap_start_y;
            //dsp_cmd.prevA_src                 = p_cvbs_dma->in_type;//SGKS_PREVIEW_SRC_TYPE_LVDS;//;
        }

        /*********
        *1:cvbs_in
        *2:mipi_in
        *3:lvds_in
        *********/
        if(dma_in_type == SGKS_PREVIEW_SRC_TYPE_MIPI)
        {
            //0:720p 1:1080p
            if(mpi->vi_device.vi_deviceinfo[i].cap_height <= 720)
            {
                dsp_cmd.mipi_resolution = 0;
            }
            else
            {
                dsp_cmd.mipi_resolution = 1;
            }

            dsp_cmd.prevA_src = SGKS_PREVIEW_SRC_TYPE_MIPI;
        }
    }
    else
    {
        dsp_cmd.prevC_src  					= SGKS_PREVIEW_SRC_TYPE_SENSOR;
        dsp_cmd.prevB_src  					= SGKS_PREVIEW_SRC_TYPE_SENSOR;
    }

    mpi_cmd_hex(dsp_cmd, cmd_code);
    mpi_cmd(dsp_cmd, input_format);
    mpi_cmd(dsp_cmd, sensor_id);
    mpi_cmd(dsp_cmd, keep_states);
    mpi_cmd(dsp_cmd, vi_frame_rate);
    mpi_cmd(dsp_cmd, vidcap_w);
    mpi_cmd(dsp_cmd, vidcap_h);
    mpi_cmd(dsp_cmd, main_w);
    mpi_cmd(dsp_cmd, main_h);
    mpi_cmd(dsp_cmd, encode_w);
    mpi_cmd(dsp_cmd, encode_h);
    mpi_cmd(dsp_cmd, encode_x);
    mpi_cmd(dsp_cmd, encode_y);
    mpi_cmd(dsp_cmd, preview_w_A);
    mpi_cmd(dsp_cmd, preview_h_A);
    mpi_cmd(dsp_cmd, input_center_x);
    mpi_cmd(dsp_cmd, input_center_y);
    mpi_cmd(dsp_cmd, zoom_factor_x);
    mpi_cmd(dsp_cmd, zoom_factor_y);
    mpi_cmd(dsp_cmd, aaa_data_fifo_start);
    mpi_cmd(dsp_cmd, sensor_readout_mode);
    mpi_cmd(dsp_cmd, noise_filter_strength);
    mpi_cmd(dsp_cmd, image_stabilize_strength);
    mpi_cmd(dsp_cmd, bit_resolution);
    mpi_cmd(dsp_cmd, bayer_pattern);
    mpi_cmd(dsp_cmd, preview_format_A);
    mpi_cmd(dsp_cmd, preview_format_B);
    mpi_cmd(dsp_cmd, no_pipelineflush);
    mpi_cmd(dsp_cmd, preview_frame_rate_A);
    mpi_cmd(dsp_cmd, preview_w_B);
    mpi_cmd(dsp_cmd, preview_h_B);
    mpi_cmd(dsp_cmd, preview_frame_rate_B);
    mpi_cmd(dsp_cmd, preview_A_en);
    mpi_cmd(dsp_cmd, preview_B_en);
    mpi_cmd(dsp_cmd, horizontal_channel_number);
    mpi_cmd(dsp_cmd, vertical_channel_number);
    mpi_cmd(dsp_cmd, vi_frame_rate_ext);
    mpi_cmd(dsp_cmd, dsp2_int_factor);
    mpi_cmd(dsp_cmd, main_out_frame_rate);
    mpi_cmd(dsp_cmd, main_out_frame_rate_ext);
    mpi_cmd(dsp_cmd, vid_skip);
    mpi_cmd(dsp_cmd, EIS_enable);
    mpi_cmd(dsp_cmd, DIS_enable);
    mpi_cmd(dsp_cmd, Vert_WARP_enable);
    mpi_cmd(dsp_cmd, no_vi_reset_exiting);
    mpi_cmd(dsp_cmd, support_cfa_out_win_2129);
    mpi_cmd(dsp_cmd, oversampling_disabled);
    mpi_cmd(dsp_cmd, hd_sdi_mode);
    mpi_cmd(dsp_cmd, reserved);
    mpi_cmd(dsp_cmd, prevA_drv_voutB);
    mpi_cmd(dsp_cmd, reserved_2);
    mpi_cmd(dsp_cmd, cmdReadDly);
    mpi_cmd(dsp_cmd, preview_src_w_A);
    mpi_cmd(dsp_cmd, preview_src_h_A);
    mpi_cmd(dsp_cmd, preview_src_x_offset_A);
    mpi_cmd(dsp_cmd, preview_src_y_offset_A);
    mpi_cmd(dsp_cmd, preview_src_w_B);
    mpi_cmd(dsp_cmd, preview_src_h_B);
    mpi_cmd(dsp_cmd, preview_src_x_offset_B);
    mpi_cmd(dsp_cmd, preview_src_y_offset_B);
    mpi_cmd(dsp_cmd, cvbs_input_width);
    mpi_cmd(dsp_cmd, cvbs_input_height);
    mpi_cmd(dsp_cmd, cvbs_interleave);
    mpi_cmd(dsp_cmd, prevC_src);
    mpi_cmd(dsp_cmd, prevA_src);
    mpi_cmd(dsp_cmd, prevB_src);
    mpi_cmd(dsp_cmd, mipi_resolution);

    ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(sgks_dsp_cmd_video_preproc_s));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }


    return SGKS_SUCCESS;

}



int sgks_mpi_vi_Cmd_CapWin(sgks_mpi_manager_s *mpi)
{

    int ret = 0;
    sgks_vi_src_capability_s       vi_cap_ability[MAX_VI_NUM];
    sgks_vi_ctrl_reg_u             sgks_vi_ctrl_reg;
    sgks_vi_inp_cfg_reg_u          sgks_inp_cfg_reg;
    sgks_dsp_cmd_vi_cap_win_s      dsp_cmd;

    CHECK_POINT_NULL_RETURN(mpi, SGKS_ERR_VI_PARAM_ERR);

    ret = sgks_mpi_vi_GetSensorCapability(vi_cap_ability);
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    sgks_vi_cap_info_s *vi_cap_info 	  = &vi_cap_ability[0].vi_cap_info;

    sgks_vi_ctrl_reg.data                 = vi_cap_info->s_ctrl_reg;
    sgks_inp_cfg_reg.data                 = vi_cap_info->s_inp_cfg_reg;

    memset(&dsp_cmd, 0, sizeof(dsp_cmd));
    dsp_cmd.cmd_code               							= SGKS_DSP_CMD_SET_VI_CAPTURE_WIN;
    dsp_cmd.S_Control_reset        							= sgks_vi_ctrl_reg.bits.S_Control_reset;
    dsp_cmd.S_Control_enable	     					    = sgks_vi_ctrl_reg.bits.S_Control_enable;     // 0: idle	1: enable video in
    //dsp_cmd.S_Control_enable	   							= 1;
    dsp_cmd.S_Control_win_en	   					        = sgks_vi_ctrl_reg.bits.S_Control_win_en;       // enable capture window. automatic reset at the end of each capture
    //dsp_cmd.S_Control_win_en	   							= 1;
    dsp_cmd.S_Control_data_edge    							= sgks_vi_ctrl_reg.bits.S_Control_data_edge;    // Data clock edge. 0: valid on rising edge of sensor clock
    dsp_cmd.S_Control_mastSlav_mod 							= sgks_vi_ctrl_reg.bits.S_Control_mastSlav_mod;	// Bit [5:4] forms the following combination:
    dsp_cmd.S_Control_data_emb_sync        					= sgks_vi_ctrl_reg.bits.S_Control_data_emb_sync;
    dsp_cmd.S_Control_data_emb_sync_mode   					= sgks_vi_ctrl_reg.bits.S_Control_data_emb_sync_mode;
    dsp_cmd.S_Control_data_emb_sync_loc    					= sgks_vi_ctrl_reg.bits.S_Control_data_emb_sync_loc;
    dsp_cmd.S_Control_data_vs_pol          					= sgks_vi_ctrl_reg.bits.S_Control_data_vs_pol;
    dsp_cmd.S_Control_data_hs_pol          					= sgks_vi_ctrl_reg.bits.S_Control_data_hs_pol;
    dsp_cmd.S_Control_data_field0_pol      					= sgks_vi_ctrl_reg.bits.S_Control_data_field0_pol;
    dsp_cmd.S_Control_data_sony_field_mode 					= sgks_vi_ctrl_reg.bits.S_Control_data_sony_field_mode;
    dsp_cmd.S_Control_data_ecc_enable     					= sgks_vi_ctrl_reg.bits.S_Control_data_ecc_enable;
    dsp_cmd.S_Control_data_hsync_mask      					= sgks_vi_ctrl_reg.bits.S_Control_data_hsync_mask;
    dsp_cmd.S_InputConfig_pad_type         					= sgks_inp_cfg_reg.bits.S_InputConfig_pad_type;      // 0: LVCMOS  1: LVDS
    dsp_cmd.S_InputConfig_data_rate        					= sgks_inp_cfg_reg.bits.S_InputConfig_data_rate;	  // 0: SDR	1: DDR
    dsp_cmd.S_InputConfig_data_width       					= sgks_inp_cfg_reg.bits.S_InputConfig_data_width;    // 0: 1-pixel wide  1: 2-pixel wide [should be programed to 1 (2-pixel wide) for serial sensor interface modes]
    dsp_cmd.S_InputConfig_input_source     					= sgks_inp_cfg_reg.bits.S_InputConfig_input_source;  // 0: from LVDS (lvds_idsp_sdata)	 1: from GPIO (iopad_idsp_sdata). Input source and pad type forms three combinations:-LVDS source, LVDS pad.-LVDS source, LVCMOS pad.-GPIO source. (Pad type makes no difference.)
    dsp_cmd.S_InputConfig_RGB_YUV	                        = sgks_inp_cfg_reg.bits.S_InputConfig_RGB_YUV;       // 0: RGB input	 1: YUV input
    dsp_cmd.S_InputConfig_Source_pixel_data_width      		= sgks_inp_cfg_reg.bits.S_InputConfig_Source_pixel_data_width;// Source pixel data width. VIN aligns all pixel values to MSB at output.
    dsp_cmd.S_InputConfig_YUV_input_order                   = sgks_inp_cfg_reg.bits.S_InputConfig_YUV_input_order;   // YUV input order
    dsp_cmd.S_InputConfig_Number_of_active_SLVS_lanes       = sgks_inp_cfg_reg.bits.S_InputConfig_Number_of_active_SLVS_lanes;   // Number of active SLVS lanes
    dsp_cmd.S_InputConfig_Serial_sensor_interface_mode      = sgks_inp_cfg_reg.bits.S_InputConfig_Serial_sensor_interface_mode;   // Serial sensor interface mode (Micron and Sony)
    dsp_cmd.S_InputConfig_Sony_serial_sensor_interface_mode = sgks_inp_cfg_reg.bits.S_InputConfig_Sony_serial_sensor_interface_mode;	// Sony serial sensor interface mode
    dsp_cmd.S_InputConfig_VIN_clock_select    				= sgks_inp_cfg_reg.bits.S_InputConfig_VIN_clock_select;   // VIN clock select - use sensor or bit clock instead of IDSP clock
    dsp_cmd.S_MIPI_Config                     				= sgks_inp_cfg_reg.bits.S_MIPI_Config;
    dsp_cmd.S_Vwidth 										= vi_cap_info->s_v_width_reg;
    dsp_cmd.S_Hwidth 										= vi_cap_info->s_h_width_reg;
    dsp_cmd.S_Hoffset_top 									= vi_cap_info->s_h_offset_top_reg;
    dsp_cmd.S_Hoffset_bot 									= vi_cap_info->s_h_offset_bot_reg;
    dsp_cmd.S_V 											= vi_cap_info->s_v_reg;
    dsp_cmd.S_H 											= vi_cap_info->s_h_reg;
    dsp_cmd.S_MinV 											= vi_cap_info->s_min_v_reg;
    //dsp_cmd.S_MinV 											= 1061;
    dsp_cmd.S_MinH 											= vi_cap_info->s_min_h_reg;
    dsp_cmd.S_Trigger0Start_startline 						= vi_cap_info->s_trigger_0_start_reg;
    dsp_cmd.S_Trigger0End_startline 						= vi_cap_info->s_trigger_0_end_reg;
    dsp_cmd.S_Trigger1Start_startline 						= vi_cap_info->s_trigger_1_start_reg;
    dsp_cmd.S_Trigger1End_startline 						= vi_cap_info->s_trigger_1_end_reg;
    dsp_cmd.S_VoutStart0_startline 							= vi_cap_info->s_vo_start_0_reg;
    dsp_cmd.S_VoutStart1_startline 							= vi_cap_info->s_vo_start_1_reg;
    dsp_cmd.S_CapStartV 									= vi_cap_info->s_cap_start_v_reg;
    //dsp_cmd.S_CapStartV 									= 2;
    dsp_cmd.S_CapStartH 									= vi_cap_info->s_cap_start_h_reg;
    dsp_cmd.S_CapEndV 										= vi_cap_info->s_cap_end_v_reg;
    //dsp_cmd.S_CapEndV 										= 1081;
    dsp_cmd.S_CapEndH 										= vi_cap_info->s_cap_end_h_reg;
    dsp_cmd.S_BlankLengthH 									= vi_cap_info->s_blank_leng_h_reg;
    dsp_cmd.S_TimeoutVLow 									= vi_cap_info->s_timeout_v_low_reg;
    dsp_cmd.S_TimeoutVHigh 									= vi_cap_info->s_timeout_v_high_reg;
    dsp_cmd.S_TimeoutHLow  									= vi_cap_info->s_timeout_h_low_reg;
    dsp_cmd.S_TimeoutHHigh 									= vi_cap_info->s_timeout_h_high_reg;
    dsp_cmd.S_mipi_cfg1 									= vi_cap_info->mipi_cfg1_reg;
    dsp_cmd.S_mipi_cfg2 									= vi_cap_info->mipi_cfg2_reg;
    dsp_cmd.S_mipi_bdphyctl 								= vi_cap_info->mipi_bdphyctl_reg;
    dsp_cmd.S_mipi_sdphyctl 								= vi_cap_info->mipi_sdphyctl_reg;
    dsp_cmd.S_MIPI_Config  									= vi_cap_info->MIPI_enable;
    dsp_cmd.S_mipi_timing1 									= vi_cap_info->mipi_timing1;
    dsp_cmd.S_mipi_timing2 									= vi_cap_info->mipi_timing2;


    mpi_cmd_hex(dsp_cmd, cmd_code);
    mpi_cmd(dsp_cmd, S_Control_reset);
    mpi_cmd(dsp_cmd, S_Control_enable);
    mpi_cmd(dsp_cmd, S_Control_win_en);
    mpi_cmd(dsp_cmd, S_Control_data_edge);
    mpi_cmd(dsp_cmd, S_Control_mastSlav_mod);
    mpi_cmd(dsp_cmd, S_Control_data_emb_sync);
    mpi_cmd(dsp_cmd, S_Control_data_emb_sync_mode);
    mpi_cmd(dsp_cmd, S_Control_data_emb_sync_loc);
    mpi_cmd(dsp_cmd, S_Control_data_vs_pol);
    mpi_cmd(dsp_cmd, S_Control_data_hs_pol);
    mpi_cmd(dsp_cmd, S_Control_data_field0_pol);
    mpi_cmd(dsp_cmd, S_Control_data_sony_field_mode);
    mpi_cmd(dsp_cmd, S_Control_data_ecc_enable);
    mpi_cmd(dsp_cmd, S_Control_data_hsync_mask);
    mpi_cmd(dsp_cmd, S_InputConfig_pad_type);
    mpi_cmd(dsp_cmd, S_InputConfig_data_rate);
    mpi_cmd(dsp_cmd, S_InputConfig_data_width);
    mpi_cmd(dsp_cmd, S_InputConfig_input_source);
    mpi_cmd(dsp_cmd, S_InputConfig_RGB_YUV);
    mpi_cmd(dsp_cmd, S_InputConfig_Source_pixel_data_width);
    mpi_cmd(dsp_cmd, S_InputConfig_YUV_input_order);
    mpi_cmd(dsp_cmd, S_InputConfig_Number_of_active_SLVS_lanes);
    mpi_cmd(dsp_cmd, S_InputConfig_Serial_sensor_interface_mode);
    mpi_cmd(dsp_cmd, S_InputConfig_Sony_serial_sensor_interface_mode);
    mpi_cmd(dsp_cmd, S_InputConfig_VIN_clock_select);
    mpi_cmd(dsp_cmd, S_MIPI_Config);
    mpi_cmd(dsp_cmd, S_Status_vsync);
    mpi_cmd(dsp_cmd, S_Status_trig0);
    mpi_cmd(dsp_cmd, S_Status_trig1);
    mpi_cmd(dsp_cmd, S_Status_ovfl);
    mpi_cmd(dsp_cmd, S_Status_shortl);
    mpi_cmd(dsp_cmd, S_Status_shortf);
    mpi_cmd(dsp_cmd, S_Status_field);
    mpi_cmd(dsp_cmd, S_Status_no_hsync);
    mpi_cmd(dsp_cmd, S_Status_no_vsync);
    mpi_cmd(dsp_cmd, S_Status_idsp_ahb_vsync);
    mpi_cmd(dsp_cmd, S_Status_idsp_ahb_mst_vsync);
    mpi_cmd(dsp_cmd, S_Status_idsp_ahb_last_pxl);
    mpi_cmd(dsp_cmd, S_Status_ecc_uncorrectable);
    mpi_cmd(dsp_cmd, S_Status_program_error);
    mpi_cmd(dsp_cmd, S_Vwidth);
    mpi_cmd(dsp_cmd, S_Hwidth);
    mpi_cmd(dsp_cmd, S_Hoffset_top);
    mpi_cmd(dsp_cmd, S_Hoffset_bot);
    mpi_cmd(dsp_cmd, S_V);
    mpi_cmd(dsp_cmd, S_H);
    mpi_cmd(dsp_cmd, S_MinV);
    mpi_cmd(dsp_cmd, S_MinH);
    mpi_cmd(dsp_cmd, S_Trigger0Start_startline);
    mpi_cmd(dsp_cmd, S_Trigger0Start_pol);
    mpi_cmd(dsp_cmd, S_Trigger0Start_enable);
    mpi_cmd(dsp_cmd, S_Trigger0End_startline);
    mpi_cmd(dsp_cmd, S_Trigger1Start_startline);
    mpi_cmd(dsp_cmd, S_Trigger1Start_pol);
    mpi_cmd(dsp_cmd, S_Trigger1Start_enable);
    mpi_cmd(dsp_cmd, S_Trigger1End_startline);
    mpi_cmd(dsp_cmd, S_VoutStart0_startline);
    mpi_cmd(dsp_cmd, S_VoutStart0_disable_field_check);
    mpi_cmd(dsp_cmd, S_VoutStart1_startline);
    mpi_cmd(dsp_cmd, S_VoutStart1_disable_field_check);
    mpi_cmd(dsp_cmd, S_CapStartV);
    mpi_cmd(dsp_cmd, S_CapStartH);
    mpi_cmd(dsp_cmd, S_CapEndV);
    mpi_cmd(dsp_cmd, S_CapEndH);
    mpi_cmd(dsp_cmd, S_BlankLengthH);
    mpi_cmd(dsp_cmd, S_TimeoutVLow);
    mpi_cmd(dsp_cmd, S_TimeoutVHigh);
    mpi_cmd(dsp_cmd, S_TimeoutHLow);
    mpi_cmd(dsp_cmd, S_TimeoutHHigh);
    mpi_cmd(dsp_cmd, S_mipi_cfg1);
    mpi_cmd(dsp_cmd, S_mipi_cfg2);
    mpi_cmd(dsp_cmd, S_mipi_bdphyctl);
    mpi_cmd(dsp_cmd, S_mipi_sdphyctl);
    mpi_cmd(dsp_cmd, S_mipi_timing1);
    mpi_cmd(dsp_cmd, S_mipi_timing2);

    ret = sgks_mpi_cmd_Send(&dsp_cmd, sizeof(sgks_dsp_cmd_vi_cap_win_s));
    if (ret != SGKS_SUCCESS)
    {
        return ret;
    }

    return SGKS_SUCCESS;

}


int sgks_mpi_vi_Deinit(void)
{
	int ret = -1;
	
	sgks_mpi_manager_s *mpi = mpi_get_managerHanle();

	ret = sgks_mpi_vi_CloseDeviceNode(mpi);
        if (ret != SGKS_SUCCESS)
            return ret;

    return SGKS_SUCCESS;

}



